<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*小型健身房管理系统对接*/
class GymApi extends  BaseApi
{
	protected $shop_id;//中未智联店铺id
	
	public function _initialize()
    {
    }
	/////////////////////////////////////////获取安科数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空(ocm)");
		}else{
			$this->shop_id=$shop_id;
		}
	}
	/**
	 * http://SmartGYM.com/api/GymApi/GetContractInfo/card_no/0723344886/shop_id/56
	 *
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 */
	function GetContractInfo($card_no,$shop_id){
		$this->before_method($shop_id);
		$json=db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->find();
		if(empty($json)){
			return template_error("卡号不存在(ocm)");
		}else{
			$member=db("GymMember")->field("id,name,sex,mobile")->find($json['gym_member_id']);
			return template_success("成功(ocm)",array(
														  "ContractID"=>$json['order_no'],
														  "MemberID"=>$json['gym_member_id'],
														  "StartDate"=>$json['start_date'],
														  "MemberName"=>$member['name'],
														  "EndDate"=>$json['end_date'],
														  "ContractStatus"=>$json['order_status'],
														  "Sex"=>$member['sex'],
														  "Mobile"=>$member["mobile"],
														  "CardNo"=>$json['card_no'],
														  "MembershipType"=>$json['gym_card_type'],
														  "MembershipTypeName"=>$json["gym_card_name"],
														  "SurplusNum"=>$json['able_count'],
														  "StoredSumValue"=>$json['able_money'],
														  )
														);	
		}
	}
	/**
	 * 入场操作
	 * http://SmartGYM.com/api/GymApi/CheckIn/card_no/123456/shop_id/22
	 * @param $card_no 卡号
	 * @param $shop_id 店铺id
	 * @param $in_type 入场方式   1闸机    2手动入场
	 */
	function CheckIn($card_no,$shop_id,$in_type="1"){
	 	$model=db("GymOrder")->where("card_no='$card_no' and shop_id='$shop_id' and is_delete=0")->find();
		if(empty($model)){
			return template_error("无效的卡号(ocm)");
		}else if($model['order_status']!=1){
			if($model['order_status']==2){
				return template_error("合同暂停中无法入场(ocm)");	
			}else if($model['order_status']==3){
					$model=db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->find();
					if($model['activate_date']<date("Y-m-d")){
						db("GymOrder")->where("id=".$model['id'])->setField(array("order_status"=>1));
					}else{
						if($model['activate_date']<$model['end_date']){
							$able_date=intval((strtotime($model['activate_date'])-strtotime(date("Y-m-d")))/(24*60*60));
							$end=date('Y-m-d',strtotime($model['end_date']." -$able_date day"));
							db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->setField(array(
										"order_status"=>1,
										"start_date"=>date('Y-m-d'),
										"end_date"=>$end,
							));	
							$model['order_status']=1;
							$model['start_date']=date('Y-m-d');
							$model['end_date']=$end;
						}
					}
			}else if($model['order_status']==4){
				if($in_type=="1"){
					return template_error("会员请假中无法入场(ocm)");	
				}else{//手动激活
					db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->setField(array(
							"order_status"=>1,
					));	
				}	
			}
		}
		if(strcmp($model['start_date'],date("Y-m-d"))>0||strcmp($model['end_date'],date("Y-m-d"))<0){
			return template_error("合同不再有效期内(ocm)");
		}else if($model['gym_card_type']==1){//次卡
			if($model['able_count']<1){
				return template_error("卡剩余次数不足(ocm)");
			}else{
				db("GymOrder")->where("card_no='$card_no' and shop_id=$shop_id and is_delete=0")->setDec('able_count');
			}
		}else if($model['gym_card_type']==3){//储值卡
			if($model['able_money']<0){
				return template_error("卡剩余金额不足(ocm)");
			}
		}else if($model['gym_card_type']==4){//错峰卡
			$time_sets=$model['time_sets'];
			if(empty($time_sets)){
				return template_error("错峰段未设置(ocm)");
			}else{
				$time_sets=explode(",", $time_sets);
				if(date("H:i")>explode("-",$time_sets[0])[0]&&date("H:i")<explode("-",$time_sets[0])[1]){
					return template_success("成功");
				}
				if(date("H:i")>explode("-",$time_sets[1])[0]&&date("H:i")<explode("-",$time_sets[1])[1]){
					return template_success("成功");
				}
			}
			return template_error("错峰段内不允许入场(ocm)"); 
		}
		return template_success("成功");
	}
	/**
	 * 出场操作
	 * @param $card_no
	 * @param $shop_id
	 * http://SmartGYM.com/api/GymApi/ConfirmCheckOut/card_no/0722757009/shop_id/22
	 */
	function ConfirmCheckOut($card_no,$shop_id){
		return template_success("成功");
	}
	function getFaceCode(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$shop_id=input("shop_id");
			if(empty($card_no)||empty($shop_id)){
				$this->api_ajax_error("","参数错误");	
			}else{
				//头部初始化获取打卡数据
			  if($card_no=='getall'){
			  	$datas['signs']=db("Sign")->where("shop_id=$shop_id and sign_date='".date('Y-m-d')."'")->order("create_time desc")->select();
				$datas['count']=db("Sign")->where("shop_id=$shop_id and sign_date='".date('Y-m-d')."'")->count(1);
				$this->api_ajax_success($datas);
			  }				  	
			  $face=db("FaceCode")->where("card_no='$card_no' and shop_id='$shop_id' ")->field("icon1,face1")->find();
			  if(empty($face)){
			  	  $this->api_ajax_error("","查询不到人脸模板");
			  }else{
			  	   $icon1=explode(",", $face['icon1'])[0];
			  	   $this->addSign($card_no,$shop_id,$icon1);
				   $datas['face1']=$face['face1'];
				   $datas['signs']=db("Sign")->where("shop_id=$shop_id and sign_date='".date('Y-m-d')."'")->order("create_time desc")->select();
				   $datas['count']=db("Sign")->where("shop_id=$shop_id and sign_date='".date('Y-m-d')."'")->count(1);
			  	   $this->api_ajax_success($datas);
			  }
			}	
		}
	}
	/**
	 * 新增签到记录
	 * */
	function addSign($card_no,$shop_id,$icon){
		$model=db("Sign")->where("shop_id=$shop_id and card_no='$card_no' and sign_date='".date('Y-m-d')."'")->fetchsql(FALSE)->find();
		if(empty($model)){
			 $member=db("Interface")->where("shop_id=$shop_id and card_no='$card_no'")->field("name,card_no,mobile")->find();
			 if(empty($member)){
			 	return $this->api_ajax_error("","用户不存在");
			 }else{
			 	db("Sign")->insert(array(
						"shop_id"=>$shop_id,
						"name"=>$member['name'],
						"card_no"=>$card_no,	
						"mobile"=>$member['mobile'],
						"sign_date"=>date("Y-m-d"),
						"icon1"=>$icon,
				));
			 }
		}else{
			$model['create_time']=date("Y-m-d H:i:s");
			db("Sign")->update($model);
		}
	}
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
